package com.example.netty

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.netty.io.core.NettyConfigure
import com.example.netty.ui.model.MainModel
import com.example.netty.ui.theme.NettyDemoTheme

private const val TAG = "MainActivity"
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: isDebug=${NettyConfigure.isDebug}")

        setContent {
            NettyDemoTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    NettyTest()
                }
            }
        }
    }
}



@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    NettyDemoTheme {
        NettyTest()
    }
}

@Composable
fun NettyTest() {
    val model = viewModel<MainModel>()
    Box(modifier = Modifier.fillMaxSize()) {

        Row {
            Column {

                Button(onClick = { model.startClient() }) {
                    Text(text = "启动客户端")
                }
                Button(onClick = { model.clientSendMessage("hello i am Client") }) {
                    Text(text = "客户端发送数据")
                }
                Button(onClick = { model.stopClient() }) {
                    Text(text = "关闭客户端")
                }
            }
            Column {
                Button(onClick = { model.startServer() }) {
                    Text(text = "启动服务端")
                }
                Button(onClick = { model.serverSendMessage("hello i am Server") }) {
                    Text(text = "服务端发送数据")
                }

            }
        }

    }
}

